package com.xe.framework;

import java.util.Stack;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

/**
 * 管理activity
 * 
 * @author xujh
 * 
 */
public class XEActivityManager {

	private Stack<Activity> mStack = null;
	
	protected XEActivityManager() {
		mStack = new Stack<Activity>();
	}
	
	/**
	 * 切换到下一个activity，
	 * 
	 * @param nextActivity
	 *            下一个activity
	 * @param extras
	 *            传递参数，不传递参数请设置为Null
	 * @param reqCode
	 * 			      返回请求的编码，不需要设置为-1
	 * @param inAnimId
	 *            进场动画
	 * @param outAnimId
	 *            离场动画
	 */
	@SuppressWarnings("rawtypes")
	public void next(Class nextActivity, Bundle extras, int reqCode, int inAnimId,
			int outAnimId) {
		Activity cur = XEApp.getInstance().getCurrentActivity();
		Intent intent = new Intent(cur, nextActivity);
		if (null != extras) {
			intent.putExtras(extras);
		}
		if( reqCode < 0 ){
			cur.startActivity(intent);
		}else{
			cur.startActivityForResult(intent, reqCode);
		}
		cur.overridePendingTransition(inAnimId, outAnimId);
		mStack.push(cur);
	}

	/**
	 * 附带结果返回上一级activity
	 * @param retCode
	 * 			      返回的的结果,Activity.ResultOK 等。
	 * @param retData
	 *            返回的数据,没有设置为null
	 * @param inAnimId
	 *            进场动画，没有动画设置为0
	 * @param outAnimId
	 *            离场动画，没有动画设置为0
	 */
	public void goBackWithResult(int retCode, Bundle retData, int inAnimId, int outAnimId) {
		if (mStack.size() >= 1) {
			Activity backAct = mStack.pop();
			Activity cur = XEApp.getInstance().getCurrentActivity();
			Intent intent = new Intent(cur, backAct.getClass());
			if(null != retData){
				intent.putExtras(retData);
			}
			cur.setResult(retCode, intent);
			cur.finish();
			cur.overridePendingTransition(inAnimId, outAnimId);
		} else {
			//throw new IllegalArgumentException("no enough activity to back");
		}
	}

	/**
	 * 返回上一级activity
	 * @param inAnimId  进场动画，没有动画设置为0
	 * @param outAnimId 离场动画，没有动画设置为0
	 */
	public void goBack(int inAnimId, int outAnimId) {
		if (mStack.size() >= 1) {
			mStack.pop();
			Activity cur = XEApp.getInstance().getCurrentActivity();
			cur.finish();
			cur.overridePendingTransition(inAnimId, outAnimId);
		} else {
			//throw new IllegalArgumentException("no enough activity to back");
		}
	}
	
	/**
	 * 返回指定的次数
	 * 
	 * @param num
	 *            返回的次数
	 * @param inAnimId
	 * @param outAnimId
	 */
	public void goBackBy(int num, int inAnimId, int outAnimId) {
		if(mStack.size() < num){
			//throw new IllegalArgumentException("no enough activity to back");
		}
		while (mStack.size() > num) {
			Activity backAct = mStack.pop();
			backAct.finish();
		}
		mStack.pop();
		
		Activity cur = XEApp.getInstance().getCurrentActivity();		
		cur.finish();
		cur.overridePendingTransition(inAnimId, outAnimId);
	}

	/**
	 * 退回到主界面
	 * 
	 * @param inAnimId
	 * @param outAnimId
	 */
	public void back2Home(int inAnimId, int outAnimId) {
		goBackBy(mStack.size(), inAnimId, outAnimId);
	}
	
}
